package com.bytz.modules.cms.auditBatch.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.auditBatch.model.AuditBatchCountModel;
import com.bytz.modules.cms.auditBatch.service.IAdminResellerProductChangeService;
import com.bytz.modules.cms.auditBatch.service.IAuditBatchResellerProductService;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.product.service.IAdminProductService;
import com.bytz.modules.cms.reseller.entity.ResellerProductChange;
import com.bytz.modules.cms.user.constants.AuditBatchConstants;
import com.bytz.modules.cms.user.entity.AuditBatch;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.List;

@RestController
@Api(tags = "经销商独享优惠申请")
@RequestMapping("/resellerProduct/change")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.ResellerProductChange.EDIT + "')")
public class ResellerProductChangeController {

    @Autowired
    private IAuditBatchResellerProductService auditBatchService;

    @Autowired
    private IAdminResellerProductChangeService changeService;

    @Autowired
    private IAdminProductService productService;

    @ApiOperation(value = "申请分页查询", notes = "申请分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<AuditBatchCountModel>> page(AuditBatch auditBatch,
                                                    @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                    @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                    HttpServletRequest req) throws Exception {

        auditBatch.setType(AuditBatchConstants.Type.RESELLER_PRODUCT);
        return Result.ok(auditBatchService.queryPage(new Page<>(pageNo, pageSize), auditBatch, req.getParameterMap()));
    }

    @ApiOperation(value = "申请查询单条", notes = "申请查询单条")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<AuditBatch> queryById(@RequestParam String id) {

        return Result.ok(auditBatchService.getById(id));
    }

    @ApiOperation(value = "申请暂存", notes = "申请暂存")
    @RequestMapping(value = "/storage", method = RequestMethod.POST)
    public Result<AuditBatch> storage(@RequestBody AuditBatch auditBatch) {

        return Result.ok(auditBatchService.storage(auditBatch));
    }

    @ApiOperation(value = "申请提交", notes = "申请提交")
    @RequestMapping(value = "/submit", method = RequestMethod.POST)
    public Result<AuditBatch> submit(@RequestBody AuditBatch auditBatch) {

        return Result.ok(auditBatchService.submit(auditBatch));
    }

    @ApiOperation(value = "申请删除", notes = "申请删除")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> deleteBatch(@RequestParam String id) {

        return Result.ok(auditBatchService.deleteBatch(id));
    }

    @ApiOperation(value = "变更记录分页查询", notes = "变更记录分页查询")
    @RequestMapping(value = "/change/list", method = RequestMethod.GET)
    public Result<IPage<ResellerProductChange>> page(ResellerProductChange change,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                                     HttpServletRequest req) {

        return Result.ok(changeService.queryPage(new Page<>(pageNo, pageSize), change, req.getParameterMap()));
    }

    @ApiOperation(value = "变更记录导入模板", notes = "变更记录导入模板")
    @RequestMapping(value = "/change/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {

        ExcelUtil.exportTemp("价格变更申请导入模板", "Sheet1", ResellerProductChange.class, response);
    }

    @ApiOperation(value = "变更记录导入", notes = "变更记录导入")
    @RequestMapping(value = "/change/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, @RequestParam String batchId, @RequestParam String resellerId) {

        List<ResellerProductChange> list = ExcelUtil.importGroup(request, ResellerProductChange.class, null);
        changeService.importExcel(batchId, resellerId, list);
        return Result.ok();
    }

    @ApiOperation(value = "变更记录添加一条", notes = "变更记录添加一条")
    @RequestMapping(value = "/change/addOne", method = RequestMethod.POST)
    public Result<Boolean> addOne(@RequestBody ResellerProductChange change) {

        return Result.ok(changeService.saveChange(change));
    }

    @ApiOperation(value = "变更记录删除", notes = "变更记录删除")
    @RequestMapping(value = "/change/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {

        return Result.ok(changeService.removeById(id));
    }

    @ApiOperation(value = "计算价格", notes = "变更记录删除")
    @RequestMapping(value = "/change/calculate", method = RequestMethod.POST)
    public Result<BigDecimal> calculateDiscountPrice(@RequestParam String resellerId, @RequestParam String productId) {

        return Result.ok(productService.getPriceByResellerIdAndProductId(resellerId, productId));
    }
}
